package com.nuoniu.sibanyun.service.so.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nuoniu.sibanyun.common.exception.NuoNiuException;
import com.nuoniu.sibanyun.common.lang.StringUtils;
import com.nuoniu.sibanyun.entity.dto.BarcodeMainInfoSearchDto;
import com.nuoniu.sibanyun.entity.erp.barcode.ErpBarcodeMainInfo;
import com.nuoniu.sibanyun.entity.so.VAreaCalculationDetail;
import com.nuoniu.sibanyun.mapper.so.VAreaCalculationDetailMapper;
import com.nuoniu.sibanyun.service.erp.barcode.IErpBarcodeInOutService;
import com.nuoniu.sibanyun.service.erp.barcode.IErpBarcodeMainInfoService;
import com.nuoniu.sibanyun.service.so.IVAreaCalculationDetailService;
import com.nuoniu.sibanyun.userThread.UserInfo;
import com.nuoniu.sibanyun.userThread.UserThreadLocal;
import com.twelvemonkeys.util.LinkedSet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 小啊刚
 * @since 2021-10-22
 */
@Service
public class VAreaCalculationDetailServiceImpl extends ServiceImpl<VAreaCalculationDetailMapper, VAreaCalculationDetail> implements IVAreaCalculationDetailService {

    @Autowired
    private IErpBarcodeMainInfoService erpBarcodeMainInfoService;
    @Autowired
    private IErpBarcodeInOutService erpBarcodeInOutService;

    @Override
    public void removeByOrderId(Long orderId) {
         baseMapper.removeByOrderId(orderId);
    }

    @Override
    public List<VAreaCalculationDetail> listByMainId(Long mainId) {
        QueryWrapper<VAreaCalculationDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_id",mainId);
        List<VAreaCalculationDetail> list = list(queryWrapper);
        return list;
    }



    @Override
    public List<VAreaCalculationDetail> analysisByBarCode(BarcodeMainInfoSearchDto barcodeMainInfoSearchDto) {
        String barcode = barcodeMainInfoSearchDto.getBarcode();
        if (StringUtils.isBlank(barcode))throw new NuoNiuException("参数错误，请传递条码");
        List<String> barcodeStrList = Arrays.asList(barcode.split(","));
        /** 校验 条码 出入库状态 */
        erpBarcodeInOutService.checkRecord(barcodeMainInfoSearchDto.getBusinessType(),barcodeStrList);
        List<ErpBarcodeMainInfo> erpBarcodeMainInfoList = erpBarcodeMainInfoService.analysisByBarCode(barcodeMainInfoSearchDto);
        List<Integer> detailIds = erpBarcodeMainInfoList.stream().filter(item -> item.getSourceVoucherType().equals("AreaCalculation")).map(ErpBarcodeMainInfo::getSourceDetailId).collect(Collectors.toList());
        if(0 == detailIds.size())return new ArrayList<>();
        QueryWrapper<VAreaCalculationDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id",detailIds);
        List<VAreaCalculationDetail> list = list(queryWrapper);
        return list;
    }

}
